home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2890 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.1 KB

  1. Path: colossus.holonet.net!russell
  2. From: russell@news.mdli.com (Russell Blackadar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: doubly dimensioned arrays
  5. Date: 20 Jan 1996 00:49:04 GMT
  6. Organization: HoloNet National Internet Access System: 510-704-1058/modem
  7. Message-ID: <4dpe60$l6k@colossus.holonet.net>
  8. References: <4dlje4$bnf@News1.mcs.net>
  9. NNTP-Posting-Host: jubal.mdli.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. stevecoh.mcs.com wrote:
  13. : how must the variable p be declared so that the following code
  14. : int m,n; ... p=new int[m][n]; ... p[m][n] 
  15. : gives the expected result, that is the int at the nth column of the mth 
  16. : row?
  17.  
  18. To use the above, at least n (and optionally m) must be const, with a
  19. value known at compile time.  You can then have the following:
  20.    const int n = 42;   // or whatever
  21.    int m;              // init as you wish
  22.    ...
  23.    int (*p)[n] = new int[m][n];   // the answer to your question
  24.     
  25. but, BTW, don't try to access p[m][n].  Remember, the last element
  26. is p[m-1][n-1].
  27.  
  28. If n cannot be a const, you need to use other techniques.  Best
  29. is to forget about arrays and use a Vector class.
  30. --
  31. Russell Blackadar,   russell@mdli.com
  32.